<?php
namespace app\api\controller;

use app\admin\model\Comment;
use app\admin\model\Intro;
use app\admin\model\Judge;
use app\admin\model\Team;
use EasyWeChat\Factory;
use Lcobucci\JWT\Builder;
use Lcobucci\JWT\Parser;
use think\Db;
use think\Request;

class Index
{
    public function teams()
    {
        $data = Team::all();
        return json(['code' => 0, 'data' => $data]);
    }

    public function team($id)
    {
        $data = Team::get($id);
        return json(['code' => 0, 'data' => $data]);
    }

    public function judges()
    {
        $data = Judge::all();
        return json(['code' => 0, 'data' => $data]);
    }

    public function judge($id)
    {
        $data = Team::get($id);
        return json(['code' => 0, 'data' => $data]);
    }

    public function comments()
    {
        $data = Comment::where('is_show', 1)->select();
        return json(['code' => 0, 'data' => $data]);
    }

    public function comment_store(Request $request)
    {
        Comment::create([
            'nickname' => $request->param('nickname'),
            'comment' => $request->param('comment'),
            'is_show' => 0,
            'created_at' => time()
        ]);
        return json(['code' => 0, 'msg' => '留言成功']);
    }

    public function intro()
    {
        $data = Intro::get(1);
        return json(['code' => 0, 'data' => $data]);
    }

    public function judge_login(Request $request)
    {
        $judge = Judge::where('account', $request->param('account'))->where('password', $request->param('password'))->find();
        if ($judge) {
            $token = (new Builder())->set('judge_id', $judge['id'])
                ->getToken();
            return json(['code' => 0, 'token' => $token->__toString()]);
        } else {
            return json(['code' => 401, 'msg' => '用户名或密码错误']);
        }
    }

    public function judge_teams(Request $request)
    {
        $token = (new Parser())->parse($request->header('authorization'));
        $judge_id = $token->getClaim('judge_id');
        if (empty($judge_id)) return json(['code' => 403, 'msg' => '未登陆']);
        $data = Db::table('teams')
            ->leftJoin('judges_teams', 'teams.id=judges_teams.team_id AND judges_teams.judge_id='.$judge_id)
            ->field([
                'teams.id' => 'id',
                'teams.name' => 'name',
                'teams.facepic' => 'facepic',
                'judges_teams.score' => 'score'
            ])
            ->select();
        return json(['code' => 0, 'data' => $data]);
    }

    public function judge_team_score(Request $request, $id)
    {
        $token = (new Parser())->parse($request->header('authorization'));
        $judge_id = $token->getClaim('judge_id');
        if (empty($judge_id)) return json(['code' => 403, 'msg' => '未登陆']);

        $judge_team_query = Db::table('judges_teams')->where('team_id', $id)->where('judge_id', $judge_id);
        if (!$judge_team_query->find()) {
            Db::table('judges_teams')->insert([
                'score' =>  $request->param('score'),
                'judge_id' =>  $judge_id,
                'team_id' =>  $id,
            ]);
        } else {
            $judge_team_query->update(['score' => $request->param('score')]);
        }
        return json(['code' => 0, 'msg' => '打分成功']);
    }

    public function jssdk()
    {
        $config = [
            'app_id' => 'wx4c4afa6fdbcd7497',
            'secret' => '6922320443c4b35a5af6a08cbe0ab130'
        ];
        $app = Factory::officialAccount($config);
        return $app->jssdk->buildConfig(['onMenuShareTimeline', 'onMenuShareAppMessage'], false);
    }
}
